<?php
	function v2e_special($value){
		$value = str_replace("â", "a", $value);
		$value = str_replace("ấ", "a", $value);
		$value = str_replace("ầ", "a", $value);
		$value = str_replace("ẩ", "a", $value);
		$value = str_replace("ẫ", "a", $value);
		$value = str_replace("ậ", "a", $value);
		#---------------------------------A^

		$value = str_replace("Â", "a", $value);
		$value = str_replace("Ấ", "a", $value);
		$value = str_replace("Ầ", "a", $value);
		$value = str_replace("Ẩ", "a", $value);
		$value = str_replace("Ẫ", "a", $value);
		$value = str_replace("Ậ", "a", $value);
		#---------------------------------a

		$value = str_replace("á", "a", $value);
		$value = str_replace("à", "a", $value);
		$value = str_replace("ả", "a", $value);
		$value = str_replace("ã", "a", $value);
		$value = str_replace("ạ", "a", $value);
		#---------------------------------A

		$value = str_replace("Á", "a", $value);
		$value = str_replace("À", "a", $value);
		$value = str_replace("Ả", "a", $value);
		$value = str_replace("Ã", "a", $value);
		$value = str_replace("Ạ", "a", $value);
		#---------------------------------a(

		$value = str_replace("ă", "a", $value);
		$value = str_replace("ắ", "a", $value);
		$value = str_replace("ằ","a", $value);
		$value = str_replace("ẳ", "a", $value);
		$value = str_replace("ẵ","a", $value);
		$value = str_replace("ặ", "a", $value);
		#---------------------------------A(

		$value = str_replace("Ă", "a", $value);
		$value = str_replace("Ắ", "a", $value);
		$value = str_replace("Ằ", "a", $value);
		$value = str_replace("Ẳ", "a", $value);
		$value = str_replace("Ẵ", "a", $value);
		$value = str_replace("Ặ", "a", $value);
		#---------------------------------e^

		$value = str_replace("ê", "e", $value);
		$value = str_replace("ế", "e", $value);
		$value = str_replace("ề", "e", $value);
		$value = str_replace("ể", "e", $value);
		$value = str_replace("ễ", "e", $value);
		$value = str_replace("ệ", "e", $value);
		#---------------------------------E^

		$value = str_replace("Ê", "e", $value);
		$value = str_replace("Ế", "e", $value);
		$value = str_replace("Ề", "e", $value);
		$value = str_replace("Ể", "e", $value);
		$value = str_replace("Ễ", "e", $value);
		$value = str_replace("Ệ", "e", $value);
		#---------------------------------e

		$value = str_replace("é","e", $value);
		$value = str_replace("è", "e", $value);
		$value = str_replace("ẻ", "e", $value);
		$value = str_replace("ẽ", "e", $value);
		$value = str_replace("ẹ", "e", $value);
		#---------------------------------E

		$value = str_replace("É", "e", $value);
		$value = str_replace("È", "e", $value);
		$value = str_replace("Ẻ", "e", $value);
		$value = str_replace("Ẽ", "e", $value);
		$value = str_replace("Ẹ", "e", $value);
		#---------------------------------i

		$value = str_replace("í", "i", $value);
		$value = str_replace("ì", "i", $value);
		$value = str_replace("ỉ", "i", $value);
		$value = str_replace("ĩ", "i", $value);
		$value = str_replace("ị", "i", $value);
		#---------------------------------I

		$value = str_replace("Í", "i", $value);
		$value = str_replace("Ì", "i", $value);
		$value = str_replace("Ỉ", "i", $value);
		$value = str_replace("Ĩ", "i", $value);
		$value = str_replace("Ị", "i", $value);
		#---------------------------------o^

		$value = str_replace("ô", "o", $value);
		$value = str_replace("ố", "o", $value);
		$value = str_replace("ồ", "o", $value);
		$value = str_replace("ổ", "o", $value);
		$value = str_replace("ỗ", "o", $value);
		$value = str_replace("ộ", "o", $value);
		#---------------------------------O^

		$value = str_replace("Ô", "o", $value);
		$value = str_replace("Ố", "o", $value);
		$value = str_replace("Ồ", "o", $value);
		$value = str_replace("Ổ", "o", $value);
		$value = str_replace("Ỗ", "o", $value);
		$value = str_replace("Ộ", "o", $value);
		#---------------------------------o*

		$value = str_replace("ơ", "o", $value);
		$value = str_replace("ớ", "o", $value);
		$value = str_replace("ờ", "o", $value);
		$value = str_replace("ở", "o", $value);
		$value = str_replace("ỡ", "o", $value);
		$value = str_replace("ợ", "o", $value);
		#---------------------------------O*

		$value = str_replace("Ơ", "o", $value);
		$value = str_replace("Ớ", "o", $value);
		$value = str_replace("Ờ", "o", $value);
		$value = str_replace("Ở", "o", $value);
		$value = str_replace("Ỡ", "o", $value);
		$value = str_replace("Ợ", "o", $value);
		#---------------------------------u*

		$value = str_replace("ư", "u", $value);
		$value = str_replace("ứ", "u", $value);
		$value = str_replace("ừ", "u", $value);
		$value = str_replace("ử", "u", $value);
		$value = str_replace("ữ", "u", $value);
		$value = str_replace("ự", "u", $value);
		#---------------------------------U*

		$value = str_replace("Ư", "u", $value);
		$value = str_replace("Ứ", "u", $value);
		$value = str_replace("Ừ", "u", $value);
		$value = str_replace("Ử", "u", $value);
		$value = str_replace("Ữ", "u", $value);
		$value = str_replace("Ự", "u", $value);
		#---------------------------------y

		$value = str_replace("ý", "y", $value);
		$value = str_replace("ỳ", "y", $value);
		$value = str_replace("ỷ", "y", $value);
		$value = str_replace("ỹ", "y", $value);
		$value = str_replace("ỵ", "y", $value);
		#---------------------------------Y

		$value = str_replace("Ý", "y", $value);
		$value = str_replace("Ỳ", "y", $value);
		$value = str_replace("Ỷ", "y", $value);
		$value = str_replace("Ỹ", "y", $value);
		$value = str_replace("Ỵ", "y", $value);
		#---------------------------------DD

		$value = str_replace("Đ", "d", $value);
		$value = str_replace("đ", "d", $value);
		#---------------------------------o

		$value = str_replace("ó", "o", $value);
		$value = str_replace("ò", "o", $value);
		$value = str_replace("ỏ", "o", $value);
		$value = str_replace("õ", "o", $value);
		$value = str_replace("ọ", "o", $value);
		#---------------------------------O

		$value = str_replace("Ó", "o", $value);
		$value = str_replace("Ò", "o", $value);
		$value = str_replace("Ỏ", "o", $value);
		$value = str_replace("Õ", "o", $value);
		$value = str_replace("Ọ", "o", $value);
		#---------------------------------u

		$value = str_replace("ú", "u", $value);
		$value = str_replace("ù", "u", $value);
		$value = str_replace("ủ", "u", $value);
		$value = str_replace("ũ", "u", $value);
		$value = str_replace("ụ", "u", $value);
		#---------------------------------U

		$value = str_replace("Ú", "u", $value);
		$value = str_replace("Ù", "u", $value);
		$value = str_replace("Ủ", "u", $value);
		$value = str_replace("Ũ", "u", $value);
		$value = str_replace("Ụ", "u", $value);
		#---------------------------------

		return $value;
	}

	function v2e($value){
		#---------------------------------SPECIAL
		$value = str_replace("&quot;","", $value);
		$value = str_replace(".","", $value);
		$value = str_replace("=","", $value);
		$value = str_replace("+","", $value);
		$value = str_replace("!","", $value);
		$value = str_replace("@","", $value);
		$value = str_replace("#","", $value);
		$value = str_replace("$","", $value);
		$value = str_replace("%","", $value);
		$value = str_replace("^","", $value);
		$value = str_replace("&","", $value);
		$value = str_replace("*","", $value);
		$value = str_replace("(","", $value);
		$value = str_replace(")","", $value);
		$value = str_replace("`","", $value);
		$value = str_replace("~","", $value);
		$value = str_replace(",","", $value);
		$value = str_replace("/","", $value);
		$value = str_replace("\\","", $value);
		$value = str_replace('"',"", $value);
		$value = str_replace("'","", $value);
		$value = str_replace(":","", $value);
		$value = str_replace(";","", $value);
		$value = str_replace("|","", $value);
		$value = str_replace("[","", $value);
		$value = str_replace("]","", $value);
		$value = str_replace("{","", $value);
		$value = str_replace("}","", $value);
		$value = str_replace("(","", $value);
		$value = str_replace(")","", $value);
		$value = str_replace("?","", $value);
		#---------------------------------a^

		$value = str_replace("â", "a", $value);
		$value = str_replace("ấ", "a", $value);
		$value = str_replace("ầ", "a", $value);
		$value = str_replace("ẩ", "a", $value);
		$value = str_replace("ẫ", "a", $value);
		$value = str_replace("ậ", "a", $value);
		#---------------------------------A^

		$value = str_replace("Â", "a", $value);
		$value = str_replace("Ấ", "a", $value);
		$value = str_replace("Ầ", "a", $value);
		$value = str_replace("Ẩ", "a", $value);
		$value = str_replace("Ẫ", "a", $value);
		$value = str_replace("Ậ", "a", $value);
		#---------------------------------a

		$value = str_replace("á", "a", $value);
		$value = str_replace("à", "a", $value);
		$value = str_replace("ả", "a", $value);
		$value = str_replace("ã", "a", $value);
		$value = str_replace("ạ", "a", $value);
		#---------------------------------A

		$value = str_replace("Á", "a", $value);
		$value = str_replace("À", "a", $value);
		$value = str_replace("Ả", "a", $value);
		$value = str_replace("Ã", "a", $value);
		$value = str_replace("Ạ", "a", $value);
		#---------------------------------a(

		$value = str_replace("ă", "a", $value);
		$value = str_replace("ắ", "a", $value);
		$value = str_replace("ằ","a", $value);
		$value = str_replace("ẳ", "a", $value);
		$value = str_replace("ẵ","a", $value);
		$value = str_replace("ặ", "a", $value);
		#---------------------------------A(

		$value = str_replace("Ă", "a", $value);
		$value = str_replace("Ắ", "a", $value);
		$value = str_replace("Ằ", "a", $value);
		$value = str_replace("Ẳ", "a", $value);
		$value = str_replace("Ẵ", "a", $value);
		$value = str_replace("Ặ", "a", $value);
		$value = str_replace("Ă", "a", $value);
		#---------------------------------e^

		$value = str_replace("ê", "e", $value);
		$value = str_replace("ế", "e", $value);
		$value = str_replace("ề", "e", $value);
		$value = str_replace("ể", "e", $value);
		$value = str_replace("ễ", "e", $value);
		$value = str_replace("ệ", "e", $value);
		#---------------------------------E^

		$value = str_replace("Ê", "e", $value);
		$value = str_replace("Ế", "e", $value);
		$value = str_replace("Ề", "e", $value);
		$value = str_replace("Ể", "e", $value);
		$value = str_replace("Ễ", "e", $value);
		$value = str_replace("Ệ", "e", $value);
		#---------------------------------e

		$value = str_replace("é","e", $value);
		$value = str_replace("è", "e", $value);
		$value = str_replace("ẻ", "e", $value);
		$value = str_replace("ẽ", "e", $value);
		$value = str_replace("ẹ", "e", $value);
		#---------------------------------E

		$value = str_replace("É", "e", $value);
		$value = str_replace("È", "e", $value);
		$value = str_replace("Ẻ", "e", $value);
		$value = str_replace("Ẽ", "e", $value);
		$value = str_replace("Ẹ", "e", $value);
		#---------------------------------i

		$value = str_replace("í", "i", $value);
		$value = str_replace("ì", "i", $value);
		$value = str_replace("ỉ", "i", $value);
		$value = str_replace("ĩ", "i", $value);
		$value = str_replace("ị", "i", $value);
		#---------------------------------I

		$value = str_replace("Í", "i", $value);
		$value = str_replace("Í", "i", $value);
		$value = str_replace("Ỉ", "i", $value);
		$value = str_replace("Ĩ", "i", $value);
		$value = str_replace("Ị", "i", $value);
		#---------------------------------o^

		$value = str_replace("ô", "o", $value);
		$value = str_replace("ố", "o", $value);
		$value = str_replace("ồ", "o", $value);
		$value = str_replace("ổ", "o", $value);
		$value = str_replace("ỗ", "o", $value);
		$value = str_replace("ộ", "o", $value);
		#---------------------------------O^

		$value = str_replace("Ô", "o", $value);
		$value = str_replace("Ố", "o", $value);
		$value = str_replace("Ồ", "o", $value);
		$value = str_replace("Ổ", "o", $value);
		$value = str_replace("Ỗ", "o", $value);
		$value = str_replace("Ộ", "o", $value);
		#---------------------------------o*

		$value = str_replace("ơ", "o", $value);
		$value = str_replace("ớ", "o", $value);
		$value = str_replace("ờ", "o", $value);
		$value = str_replace("ở", "o", $value);
		$value = str_replace("ỡ", "o", $value);
		$value = str_replace("ợ", "o", $value);
		#---------------------------------O*

		$value = str_replace("Ơ", "o", $value);
		$value = str_replace("Ớ", "o", $value);
		$value = str_replace("Ờ", "o", $value);
		$value = str_replace("Ở", "o", $value);
		$value = str_replace("Ỡ", "o", $value);
		$value = str_replace("Ợ", "o", $value);
		#---------------------------------u*

		$value = str_replace("ư", "u", $value);
		$value = str_replace("ứ", "u", $value);
		$value = str_replace("ừ", "u", $value);
		$value = str_replace("ử", "u", $value);
		$value = str_replace("ữ", "u", $value);
		$value = str_replace("ự", "u", $value);
		#---------------------------------U*

		$value = str_replace("Ư", "u", $value);
		$value = str_replace("Ứ", "u", $value);
		$value = str_replace("Ừ", "u", $value);
		$value = str_replace("Ử", "u", $value);
		$value = str_replace("Ữ", "u", $value);
		$value = str_replace("Ự", "u", $value);
		#---------------------------------y

		$value = str_replace("ý", "y", $value);
		$value = str_replace("ỳ", "y", $value);
		$value = str_replace("ỷ", "y", $value);
		$value = str_replace("ỹ", "y", $value);
		$value = str_replace("ỵ", "y", $value);
		#---------------------------------Y

		$value = str_replace("Ý", "y", $value);
		$value = str_replace("Ỳ", "y", $value);
		$value = str_replace("Ỷ", "y", $value);
		$value = str_replace("Ỹ", "y", $value);
		$value = str_replace("Ỵ", "y", $value);
		#---------------------------------DD

		$value = str_replace("Đ", "d", $value);
		$value = str_replace("đ", "d", $value);
		#---------------------------------o

		$value = str_replace("ó", "o", $value);
		$value = str_replace("ò", "o", $value);
		$value = str_replace("ỏ", "o", $value);
		$value = str_replace("õ", "o", $value);
		$value = str_replace("ọ", "o", $value);
		#---------------------------------O

		$value = str_replace("Ó", "o", $value);
		$value = str_replace("Ò", "o", $value);
		$value = str_replace("Ỏ", "o", $value);
		$value = str_replace("Õ", "o", $value);
		$value = str_replace("Ọ", "o", $value);
		#---------------------------------u

		$value = str_replace("ú", "u", $value);
		$value = str_replace("ù", "u", $value);
		$value = str_replace("ủ", "u", $value);
		$value = str_replace("ũ", "u", $value);
		$value = str_replace("ụ", "u", $value);
		#---------------------------------U

		$value = str_replace("Ú", "u", $value);
		$value = str_replace("Ù", "u", $value);
		$value = str_replace("Ủ", "u", $value);
		$value = str_replace("Ũ", "u", $value);
		$value = str_replace("Ụ", "u", $value);
		#---------------------------------

		return $value;
	}

	if ( ! function_exists('field'))
	{
		function field($str=''){
			$CI =& get_instance();
			$lang= $CI->uri->segment(1);
			return $str.'_'.$lang;
		}
	}

	if ( ! function_exists('SEO')){
		function SEO($name='') {
			$name = v2e($name);
			$name = preg_replace("/[^a-z,A-Z,0-9,_,-]/", "-", $name);
			$name = str_replace("---", "-", $name);
			$name = str_replace("--", "-", $name);
			return strtolower($name);
		}
	}

	if ( ! function_exists('img'))
	{
		function img($image_path, $width = 0, $height = 0) {
			//Get the Codeigniter object by reference
			$CI = & get_instance();
			 
			//Alternative image if file was not found
			if (! file_exists($image_path) || !is_file($image_path)){
				$image_path = 'static/images/img_item_home.jpg';
			}
				
			//The new generated filename we want
			$fileinfo = pathinfo($image_path);
			//MAKE A FOLDER
			$uploadDir_Thumb= $fileinfo['dirname'].'/'.$width.'x'.$height.'/';
			Newfolder($uploadDir_Thumb);
		
			$new_image_path = $uploadDir_Thumb . $fileinfo['filename'] . '.' . $fileinfo['extension'];	 
			//The first time the image is requested
			//Or the original image is newer than our cache image
			if ((! file_exists($new_image_path)) || filemtime($new_image_path) < filemtime($image_path)) {
				$CI->load->library('image_lib');
				//The original sizes
				$original_size = getimagesize($image_path);
				$original_width = $original_size[0];
				$original_height = $original_size[1];
				$ratio = $original_width / $original_height;
				//The requested sizes
				$requested_width = $width;
				$requested_height = $height;
				
				//Initialising
				$new_width = 0;
				$new_height = 0;
				
				//Calculations
				if ($requested_width > $requested_height) {
					$new_width = $requested_width;
					$new_height = $new_width / $ratio;
					if ($requested_height == 0)
						$requested_height = $new_height;
					
					if ($new_height < $requested_height) {
						$new_height = $requested_height;
						$new_width = $new_height * $ratio;
					}
				}else {
					$new_height = $requested_height;
					$new_width = $new_height * $ratio;
					if ($requested_width == 0)
						$requested_width = $new_width;
					
					if ($new_width < $requested_width) {
						$new_width = $requested_width;
						$new_height = $new_width / $ratio;
					}
				}
				
				$new_width = ceil($new_width);
				$new_height = ceil($new_height);
				
				//Resizing
				$config = array();
				$config['image_library'] = 'gd2';
				$config['source_image'] = $image_path;
				$config['new_image'] = $new_image_path;
				$config['maintain_ratio'] = FALSE;
				$config['height'] = $new_height;
				$config['width'] = $new_width;
				$CI->image_lib->initialize($config);
				$CI->image_lib->resize();
				$CI->image_lib->clear();
				
				//Crop if both width and height are not zero
				if (($width != 0) && ($height != 0)) {
					$x_axis = floor(($new_width - $width) / 2);
					$y_axis = floor(($new_height - $height) / 2);
					
					//Cropping
					$config = array();
					$config['source_image'] = $new_image_path;
					$config['maintain_ratio'] = FALSE;
					$config['new_image'] = $new_image_path;
					$config['width'] = $width;
					$config['height'] = $height;
					$config['x_axis'] = $x_axis;
					$config['y_axis'] = $y_axis;
					$config['quality'] = '100%';
					$CI->image_lib->initialize($config);
					$CI->image_lib->crop();
					$CI->image_lib->clear();
				}
			}
			 
			return PATH_URL.$new_image_path;
		}	
	}

	if ( ! function_exists('SEO_TITLE'))
	{
		function SEO_TITLE($content='') {
			$content= trim($content);
			$content = str_replace("'",'',$content);
			$content = str_replace("’",'',$content);
			$content = str_replace('"','',$content);
			$content = str_replace('“','',$content);
			$content = str_replace('”','',$content);
			return $content.' | '.lang('sitename');
		}
	}

	if ( ! function_exists('title'))
	{
		function title($str=''){
			$str= htmlspecialchars(trim($str));
			return $str;
		}
	}

	if ( ! function_exists('change_lang'))
	{
		function change_lang($result=''){
			$CI =& get_instance();
			$lang= $CI->uri->segment(1);
			if($lang=='vi') {
				$lang='en';$language='English';
			}elseif($lang=='en'){
				$lang='vi';$language='Tiếng việt';
			}
			$title_lang= 'title_'.$lang;
			if(!empty($result->$title_lang)){
				$link_lang= 'link_'.$lang;
				$html= '<a href="'.PATH_URL.$lang.'/'.$result->$link_lang.'" title="'.$language.'">'.$language.'</a>';
			}else{
				$html= '<a href="'.PATH_URL.$lang.'" title="'.$language.'">'.$language.'</a>';
			}
			return $html;
		}
	}

	if ( ! function_exists('link_url'))
	{
		function link_url(){
			$CI =& get_instance();
			$current_url= current_url();
			$link= str_replace(PATH_URL_LANG, '', $current_url);
			return $link;
		}
	}

	if ( ! function_exists('random_string')){
		function random_string($length = 4)
		{
			$sWord = '';
			$sChars = 'abcdefghjklmnprtwyzABCDEFGHJKLMNPRTWXYZ1234567890';
			for ($i = 1; $i <= $length; $i++)
			{
				$nNumber = rand(1, strlen($sChars));
				$sWord .= substr($sChars, $nNumber - 1, 1);
			}
			return $sWord;
		}
	}

	if ( ! function_exists('pr')){
		function pr($data,$type=0)
		{
			print '<pre>';
			print_r($data);
			print '</pre>';
			if($type!=0){
				exit();
			}
		}
	}

	if ( ! function_exists('CutText')){
		function CutText($text, $n=80)
		{
			// string is shorter than n, return as is
			if (strlen($text) <= $n) {
				return $text;}
			$text= substr($text, 0, $n);
			if ($text[$n-1] == ' ') {
				return trim($text)."...";
			}
			$x  = explode(" ", $text);
			$sz = sizeof($x);
			if ($sz <= 1)   {
				return $text."...";}
			$x[$sz-1] = '';
			return trim(implode(" ", $x))."...";
		}
	}

	function getIP(){
		if (!empty($_SERVER['HTTP_CLIENT_IP']))  //check ip from share internet
		{
		  $ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
		//to check ip is pass from proxy
		{
		  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else
		{
		  $ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

	function resizeImage($src,$w=0,$h=0){
		if($w!=0 && $h!=0){
			return PATH_URL.'imgresize.php?width='.$w.'&height='.$h.'&cropratio=1:1&image='.$src;
		}elseif($w!=0){
			return PATH_URL.'imgresize.php?width='.$w.'&image='.$src;
		}elseif($h!=0){
			return PATH_URL.'imgresize.php?height='.$h.'&image='.$src;
		}
	}

	function check_dir_upload($targetDir){
		if(!is_dir($targetDir.date('Y'))){
			@mkdir($targetDir.date('Y'),0777);
		}
		if(!is_dir($targetDir.date('Y').'/'.date('m'))){
			@mkdir($targetDir.date('Y').'/'.date('m'),0777);
		}
		if(!is_dir($targetDir.date('Y').'/'.date('m').'/'.date('d'))){
			@mkdir($targetDir.date('Y').'/'.date('m').'/'.date('d'),0777);
		}
	}

	function parserEditorHTML($str){
		$str = str_replace('../../../static/uploads/editor/', PATH_URL.'static/uploads/editor/', $str);
		$str = preg_replace('/http:\/\/www.youtube.com\/watch\?v=([A-Za-z0-9\-\_]+)&amp;feature=([A-Za-z0-9]+)/is', '<iframe width="500" height="289" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $str);
		$str = preg_replace('/http:\/\/www.youtube.com\/watch\?v=([A-Za-z0-9\-\_]+)/is', '<iframe width="500" height="289" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $str);
		return $str;
	}

	function url_login_openid($type='google'){
		if($type=='google'){
			$params = array(
				'response_type'            => 'token',
				'client_id'                => G_CLIENT_ID,
				'redirect_uri'             => REDIRECT_URL,
				'state'                    => 'profile',
				'scope'                    => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.google.com/m8/feeds'
			);

			$url = 'https://accounts.google.com/o/oauth2/auth?';
			foreach($params as $key=>$param){
				$url .= $key.'='.urlencode($param).'&';
			}
		}
		if($type=='yahoo'){
			$params = array(
				'openid.claimed_id'        => 'http://specs.openid.net/auth/2.0/identifier_select',
				'openid.identity'          => 'http://specs.openid.net/auth/2.0/identifier_select',
				'openid.mode'              => 'checkid_setup',
				'openid.ns'                => 'http://specs.openid.net/auth/2.0',
				'openid.realm'             => PATH_URL,
				'openid.return_to'         => REDIRECT_URL.'/login/yahoo',
				'openid.ns.oauth'          => 'http://specs.openid.net/extensions/oauth/1.0',
				'openid.oauth.consumer'    => YH_CLIENT_KEY,
				'openid.ns.ax'             => 'http://openid.net/srv/ax/1.0',
				'openid.ax.mode'           => 'fetch_request',
				'openid.ax.required'       => 'email,fullname,nickname,gender,language,timezone,image',
				'openid.ax.type.email'     => 'http://axschema.org/contact/email',
				'openid.ax.type.fullname'  => 'http://axschema.org/namePerson',
				'openid.ax.type.nickname'  => 'http://axschema.org/namePerson/friendly',
				'openid.ax.type.gender'    => 'http://axschema.org/person/gender',
				'openid.ax.type.image'     => 'http://axschema.org/media/image/default',
			);

			$url = 'https://open.login.yahooapis.com/openid/op/auth?';
			foreach($params as $key=>$param){
				$url .= $key.'='.urlencode($param).'&';
			}
		}
		if($type=='fb'){
			$url = 'https://www.facebook.com/dialog/oauth/?client_id='.FB_CLIENT_ID.'&redirect_uri='.REDIRECT_URL.'&state='.FB_CLIENT_SECRET.'&display=popup&scope=email,publish_stream';
		}
		return $url;
	}

	function get_attr_profile_openid($type){
		if($type=='google'){
			include('application/libraries/eac_curl.class.php');
			$options = array();
			$options['CURLOPT_AUTOREFERER']    = 1;
			$options['CURLOPT_CRLF']           = 1;
			$options['CURLOPT_NOPROGRESS']     = 1;
			$http = new cURL($options);
			$http->setOptions($options);
			$src = $http->get("https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$_GET['access_token']);
			$contact = $http->get("https://www.google.com/m8/feeds/contacts/default/full?max-results=5000&oauth_token=".$_GET['access_token']);
			$xml =  new SimpleXMLElement($contact);
			$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
			$result = $xml->xpath('//gd:email');

			$profile = json_decode($src);
			$info['type'] = 'google';
			if(isset($profile->id)){
				$info['id'] = $profile->id;
			}else{
				$info['id'] = '';
			}
			if($profile->email){
				$info['email'] = $profile->email;
			}else{
				$info['email'] = '';
			}
			if($profile->verified_email){
				$info['verified_email'] = $profile->verified_email;
			}else{
				$info['verified_email'] = '';
			}
			if($profile->name){
				$info['full_name'] = $profile->name;
			}else{
				$info['full_name'] = '';
			}
			if($profile->given_name){
				$info['f_name'] = $profile->given_name;
			}else{
				$info['f_name'] = '';
			}
			if($profile->family_name){
				$info['l_name'] = $profile->family_name;
			}else{
				$info['l_name'] = '';
			}
			if(isset($profile->picture)){
				$info['avatar'] = $profile->picture;
			}else{
				$info['avatar'] = '';
			}
			if(isset($profile->locale)){
				$info['locale'] = $profile->locale;
			}else{
				$info['locale'] = '';
			}
			if(isset($profile->timezone)){
				$info['timezone'] = $profile->timezone;
			}else{
				$info['timezone'] = '';
			}
			if(isset($profile->gender)){
				$info['gender'] = $profile->gender;
			}else{
				$info['gender'] = '';
			}
			foreach ($result as $title){
				$info['contact'][] = $title->attributes()->address;
			}
		}elseif($type=='yahoo'){
			$info['type'] = 'yahoo';
			if(isset($_REQUEST['openid_ax_value_email'])){
				$info['email'] = $_REQUEST['openid_ax_value_email'];
			}else{
				$info['email'] = '';
			}

			if(isset($_REQUEST['openid_ax_value_fullname'])){
				$info['full_name'] = $_REQUEST['openid_ax_value_fullname'];
			}else{
				$info['full_name'] = '';
			}

			if(isset($_REQUEST['openid_ax_value_nickname'])){
				$info['nick_name'] = $_REQUEST['openid_ax_value_nickname'];
			}else{
				$info['nick_name'] = '';
			}

			if(isset($_REQUEST['openid_ax_value_gender'])){
				$info['gender'] = $_REQUEST['openid_ax_value_gender'];
			}else{
				$info['gender'] = '';
			}

			if(isset($_REQUEST['openid_ax_value_image'])){
				$info['avatar'] = $_REQUEST['openid_ax_value_image'];
			}else{
				$info['avatar'] = '';
			}
		}else{
			$facebook = new Facebook(array(
				'appId'  => FB_CLIENT_ID,
				'secret' => FB_CLIENT_SECRET,
			));
			$user_id = $facebook->getUser();
			if($user_id){
				$user_info = $facebook->api('/me');
				$list_firend = $facebook->api('/me/friends');
				$info['type'] = 'facebook';
				$info['uid'] = $user_info['id'];
				$info['email'] = $user_info['email'];
				$info['full_name'] = $user_info['name'];
				$info['list_firend'] = $list_firend;
			}
		}
		return $info;
	}

	if( ! function_exists('getWeek'))
	{
		function getWeek()
		{
			$current_week 	= 1;
			$current_date	= mktime(date('H'),date('i'),date('s'),date('m'),date('d'),date('Y'));
			$week_1_start 	= mktime('00','00','00','11','01','2012');
			$week_1_end 	= mktime('23','59','59','11','07','2012');
			$week_2_start 	= mktime('00','00','00','11','08','2012');
			$week_2_end 	= mktime('23','59','59','11','14','2012');
			$week_3_start 	= mktime('00','00','00','11','15','2012');
			$week_3_end 	= mktime('23','59','59','11','21','2012');
			$week_4_start 	= mktime('00','00','00','11','22','2012');
			$week_4_end 	= mktime('23','59','59','11','30','2012');
			$week_5_start 	= mktime('00','00','00','11','22','2012');
			$week_5_end 	= mktime('23','59','59','11','30','2012');
			$week_6_start 	= mktime('00','00','00','11','22','2012');
			$week_6_end 	= mktime('23','59','59','11','30','2012');
			$week_7_start 	= mktime('00','00','00','11','22','2012');
			$week_7_end 	= mktime('23','59','59','11','30','2012');
			$week_8_start 	= mktime('00','00','00','11','22','2012');
			$week_8_end 	= mktime('23','59','59','11','30','2012');

			if($current_date >= $week_1_start && $current_date <= $week_1_end)
			{
				$current_week = 1;
			}elseif($current_date >= $week_2_start && $current_date <= $week_2_end)
			{
				$current_week = 2;
			}elseif($current_date >= $week_3_start && $current_date <= $week_3_end)
			{
				$current_week = 3;
			}elseif($current_date >= $week_4_start && $current_date <= $week_4_end)
			{
				$current_week = 4;
			}elseif($current_date >= $week_5_start && $current_date <= $week_5_end)
			{
				$current_week = 5;
			}elseif($current_date >= $week_6_start && $current_date <= $week_6_end)
			{
				$current_week = 6;
			}elseif($current_date >= $week_7_start && $current_date <= $week_7_end)
			{
				$current_week = 7;
			}elseif($current_date >= $week_8_start && $current_date <= $week_8_end)
			{
				$current_week = 8;
			}else
			{
				$current_week = 1;
			}

			return $current_week;
		}
	}

	if ( ! function_exists('getNow'))
	{
		function getNow()
		{
			return date(DATETIME_FORMAT_DB);
		}
	}

	if ( ! function_exists('last_query'))
	{
		function last_query($exit=false)
		{
			$CI =& get_instance();
			echo $CI ->db->last_query();
			if($exit){
				exit();
			}
		}
	}

	if( ! function_exists('is_file'))
	{
		function is_file($link_file)
		{
			$img_temp = null;
			if(file_exists($link_file))
			{
				$img_temp = $link_file;
			}else{
				$img_temp = 'default.jpg';
			}
			return $img_temp;
		}
	}

	if( ! function_exists('validate_length'))
	{
		function validate_length($string, $min=NULL, $max=NULL, $is_number=true){
			$str_length = strlen($string);
			if($min != NULL && $max != NULL){
				if($str_length >= $min && $str_length <= $max){
					return true;
				}else{
					return false;
				}
			}elseif ($min != NULL) {
				if($str_length >= $min){
					return true;
				}else{
					return false;
				}
			}else{
				if($str_length <= $max){
					return true;
				}else{
					return false;
				}
			}
		}
	}

	/******
	 * Kiem tra du lieu email
	 * Return : Boolean
	 */
	if ( ! function_exists('validate_email')){
		function validate_email($email){
			return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? FALSE : TRUE;
		}
	}

	if ( ! function_exists('contest_upload_image')){
		function contest_upload_image($file = '',$dir_file,$width,$height){
			$CI =& get_instance();
			$CI->load->library('image_lib');

			$config_thumb_fb = array();
			$config_thumb_fb['image_library'] = 'gd2';
			$config_thumb_fb['source_image'] = DIR_CONTEST . $file;
			$config_thumb_fb['new_image'] = $dir_file . $file;
			$config_thumb_fb['create_thumb'] = FALSE;
			$config_thumb_fb['maintain_ratio'] = TRUE;
			$config_thumb_fb['width'] = $width;
			$config_thumb_fb['height'] = $height;
			$this->image_lib->initialize($config_thumb_fb);
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}

	if ( ! function_exists('Newfolder'))
	{
		function Newfolder($folder) {
			$arr_folder= explode('/', $folder);
			$fol='';
			foreach($arr_folder as $row)
			{
				if(!empty($row))
				{
					$fol.=$row.'/';
					if(!file_exists($fol))
					{
						@mkdir($fol, 0777);
					}
					else
					{
						$mod= substr(sprintf('%o', fileperms($fol)), -4);
						if($mod!=0777)
						{
							@chmod($fol, 0777);
						}
					}
				}
			}
		}
	}

	if ( ! function_exists('get_folder'))
	{
		function get_folder() {
			$year= date('Y');$month= date('m');
			return $year.'/'.$month.'/';
		}
	}

	if ( ! function_exists('get_day_week'))
	{
		function get_day_week() {
			$result = '';
			$num_day= date('w');
			switch ($num_day) {
				case '0':
					$result = 'Chủ nhật';
					break;
				case '1':
					$result = 'Thứ 2';
					break;
				case '2':
					$result = 'Thứ 3';
					break;
				case '3':
					$result = 'Thứ 4';
					break;
				case '4':
					$result = 'Thứ 5';
					break;
				case '5':
					$result = 'Thứ 6';
					break;
				case '6':
					$result = 'Thứ 7';
					break;

				default:
					# code...
					break;
			}
			return $result;
		}
	}

	if(!function_exists('pagelistLimited')) {
	function pagelistLimited($totalRows, $pageNum= 1, $pageSize, $limit= 3, $function = '') {
			settype($totalRows, "int");settype($pageSize, "int");
			if($totalRows <= 0) return "";
			$totalPages= ceil($totalRows / $pageSize);
			if($totalPages <= 1) return "";
			$currentPage= $pageNum;
			if($currentPage <= 0 || $currentPage > $totalPages) $currentPage= 1;

			//From to
			$form= $currentPage - $limit;
			$to= $currentPage + $limit;

			$prev_lang = lang('prev');
			$next_lang = lang('next');

			//Tinh toan From to
			if($form <= 0) {$form= 1; $to= $limit*2;};
			if($to > $totalPages) $to= $totalPages;

			//Tinh toan nut first prev next last
			$first= '';$prev= '';$next= '';$last= '';$link= '';

			//Link URL
			$linkUrl= current_url();

			$get='';$querystring='';
			if($_GET) {
				foreach($_GET as $k => $v) {
					if ($k != 'p') $querystring= $querystring . "&{$k}={$v}";
				}
				$querystring = substr($querystring,1);

				$get= $get.'?'.$querystring;
			}
			$sep= (!empty($querystring))?'&':'';
			$linkUrl= $linkUrl.'?'.$querystring.$sep.'p=';

			if($currentPage > $limit+2) {
			/** first */
				if($function == ''){
					$first = "<a href=\"$linkUrl\" class=\"first\"><img src=\"".PATH_URL.'static/images/icon/first.png'."\"></a>";
				}
				else{
					$first = '<a href="javascript:;" class="first" onclick="ajaxPaging(\'1\',$(this))"><img src="'.PATH_URL.'static/images/icon/first.png"></a>';
				}
			}

			/****** prev ***/
			if($currentPage > 1) {
				$prevPage= $currentPage - 1;
				if($function == ''){
					$prev = "<li><a href=\"$linkUrl$prevPage\" class=\"prev\">$prev_lang</a></li>";
				}
				else{
					$prev = "<li><a href=\"javascript:;\" onclick=\"ajaxPaging($prevPage,$(this))\" class=\"prev\">$prev_lang</a></li>";
				}
			}

			/***Next***/
			if($currentPage < $totalPages) {
				$nextPage= $currentPage +1;
				if($function == ''){
					$next= "<li><a href=\"$linkUrl$nextPage\" class=\"next\">$next_lang</a></li>";
				}
				else{
					$tmp = $currentPage+1;
					$next = "<li><a href=\"javascript:;\" onclick=\"ajaxPaging($tmp,$(this))\" class=\"next\">$next_lang</a></li>";
				}
			}

			/***Last***/
			if($currentPage < $totalPages - 4) {
				$lastPage = $totalPages;
				if($function == ''){
					$last = "<li><a href=\"$linkUrl$lastPage\" class=\"last\"><img src=\"".PATH_URL.'static/images/icon/last.png'."\"></a></li>";
				}
				else{
					$last = "<li><a href=\"javascript:;\" onclick=\"ajaxPaging($lastPage,$(this))\" class=\"last\"><img src=\"".PATH_URL.'static/images/icon/last.png'."\"></a></li>";
				}
			}

			/***Link***/
			for($i= $form; $i <= $to; $i++) {
				if($currentPage == $i) $link.= "<li class='active'><a href='javascript:;'>".$i."</a></li>";
				elseif($function == ''){
					$link.= "<li><a class=\"page_num\" href=\"$linkUrl$i\">$i</a></li>";
				}
				else{
					$link.= "<li><a class=\"page_num\" href=\"javascript:;\" onclick=\"ajaxPaging($i,$(this))\">$i</a></li>";
				}
			}

			$pagination= '<div class="paging"><ul>'.$first.$prev.$link.$next.$last.'</ul></div>';

			return $pagination;

	}//pagelistLimited

	if ( ! function_exists('getSiteName'))
	{
		function getSiteName(){
			$retval = '';
			$CI = & get_instance();
			$CI->db->where('slug','title-admincp');
			$row = $CI->db->get('admin_nqt_settings')->row();
			if(!empty($row))
				$retval = $row->content;
			return $retval;
		}
	}

	//check status yahoo
	function statusYH($id = ''){
		$ch = curl_init("http://opi.yahoo.com/online?u=".$id."&m=t");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$status = curl_exec($ch);
		curl_close($ch);
		if($status == $id.' is NOT ONLINE')
			$status = FALSE;
		elseif($status == $id.' is ONLINE')	
			$status = TRUE;
		return $status;
	}

	function statusSkype($id = ''){
		// $ch = curl_init("http://mystatus.skype.com/".$id.".num");
		// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// pr($ch);
		// $status = curl_exec($ch);
		// curl_close($ch);
		// if($status == $id.' is NOT ONLINE')
		// 	$status = FALSE;
		// elseif($status == $id.' is ONLINE')	
		// 	$status = TRUE;
		// return $status;
		$remote_status = fopen ('http://mystatus.skype.com/'.$id.'.num', 'r');
	    if (!$remote_status) {
	        return '0';
	        exit;
	    }
	    while (!feof ($remote_status)) {
	        $value = fgets ($remote_status, 1024);
	        return trim($value);
	    }

	    fclose($remote_status);
	}
}
?>
